/*
 * VentCosto.java
 *
 * Created on 7 de noviembre de 2007, 05:56 PM
 */

package administrador;
import javax.swing.*; 
import java.sql.*;
/**
 *
 * @author  daniel1
 */
public class VentCosto extends javax.swing.JFrame {
    String codigo;
    Conexion conexion;
    Productos producto;
    VentCreaProd ventcreaprod;
    String ventana;
    /** Creates new form VentCosto */
    public VentCosto(String codigo,Conexion c,Productos vEnt) {
        producto=vEnt;
        this.codigo=codigo;
        conexion=c;
        initComponents();
        setBounds(600,55,330,330);
        setVisible(true);
        llenoVentanaProd(codigo);
        calcularPrecio();
        ventana="Producto";
    }
    public VentCosto(String codigo,Conexion c,VentCreaProd vEnt) {
        ventcreaprod=vEnt;
        this.codigo=codigo;
        conexion=c;
        initComponents();
        setBounds(600,55,330,330);
        setVisible(true);
        llenoVentanaCrea(codigo);
        calcularPrecio();
        ventana="VentCreaProd";
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txt_precio = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txt_costo = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        txt_nuevo = new javax.swing.JTextField();
        cb_opciones = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        bt_guardar = new javax.swing.JButton();
        txt_codigo = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        jPanel1.setLayout(null);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Costo del Producto"));
        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24));
        jLabel1.setText("Costo");
        jPanel1.add(jLabel1);
        jLabel1.setBounds(20, 20, 68, 29);

        jLabel2.setForeground(new java.awt.Color(153, 0, 0));
        jLabel2.setText("Producto");
        jPanel1.add(jLabel2);
        jLabel2.setBounds(70, 50, 43, 14);

        txt_precio.setEditable(false);
        jPanel1.add(txt_precio);
        txt_precio.setBounds(100, 110, 100, 20);

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel3.setText("Precio Actual:");
        jPanel1.add(jLabel3);
        jLabel3.setBounds(20, 110, 90, 20);

        txt_costo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txt_costoKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txt_costoKeyTyped(evt);
            }
        });

        jPanel1.add(txt_costo);
        txt_costo.setBounds(100, 80, 100, 20);

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel5.setText("Precio Costo:");
        jPanel1.add(jLabel5);
        jLabel5.setBounds(20, 80, 90, 20);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Recalcular Precio"));
        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel4.setText("Precio Nuevo:");

        txt_nuevo.setFont(new java.awt.Font("Tahoma", 0, 18));
        txt_nuevo.setForeground(new java.awt.Color(102, 102, 255));

        cb_opciones.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Ninguno", "Ganar 100%", "Ganar 150%", "Ganar 200%" }));
        cb_opciones.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cb_opcionesActionPerformed(evt);
            }
        });

        jLabel6.setText("Opciones de Calculo:");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cb_opciones, 0, 240, Short.MAX_VALUE)
                    .addComponent(jLabel6)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txt_nuevo, javax.swing.GroupLayout.DEFAULT_SIZE, 146, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cb_opciones, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(16, 16, 16)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txt_nuevo, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jPanel1.add(jPanel2);
        jPanel2.setBounds(10, 150, 290, 140);

        bt_guardar.setText("Guardar");
        bt_guardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_guardarActionPerformed(evt);
            }
        });

        jPanel1.add(bt_guardar);
        bt_guardar.setBounds(210, 73, 90, 60);

        txt_codigo.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        jPanel1.add(txt_codigo);
        txt_codigo.setBounds(190, 34, 110, 20);

        jLabel7.setText("Codigo:");
        jPanel1.add(jLabel7);
        jLabel7.setBounds(150, 40, 60, 14);

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txt_costoKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txt_costoKeyReleased
// TODO add your handling code here:
        calcularPrecio();
    }//GEN-LAST:event_txt_costoKeyReleased

    private void txt_costoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txt_costoKeyTyped
// TODO add your handling code here:
   calcularPrecio();
    }//GEN-LAST:event_txt_costoKeyTyped

    private void cb_opcionesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cb_opcionesActionPerformed
// TODO add your handling code here:
     calcularPrecio();
    }//GEN-LAST:event_cb_opcionesActionPerformed

    private void bt_guardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_guardarActionPerformed
// TODO add your handling code here:
        String costo=txt_costo.getText();
        String precio=txt_nuevo.getText();
        String codigo=txt_codigo.getText();
        
         dispose();
         
         //Opciones de las diferentes Ventanas:
         if(ventana.equals("Producto")){
             //Modifico Prodcuto:
             int opccion=0;
             opccion=JOptionPane.showConfirmDialog(this,"Seguro Quiere Modificar el Producto cod= "+codigo+" Precio="+precio+",Costo="+costo,"Alerta",0);
             if(opccion==0 ){
              conexion.actualizarBase("update producto set precio_normal="+precio+", precio_costo="+costo+"  where cod_producto ='"+codigo+"';");
             }
             producto.muestraProducto();
         }
         if(ventana.equals("VentCreaProd")){
         ventcreaprod.txt_costo.setText(costo);
         ventcreaprod.txt_precio.setText(precio);
         }
         
    }//GEN-LAST:event_bt_guardarActionPerformed
    public void llenoVentanaProd(String codigo){
    ResultSet resp=conexion.consultarBase("select * from producto where cod_producto like '"+codigo+"'");
    txt_codigo.setText(codigo);
    try {
          resp.beforeFirst();
          resp.next();
          double normal=resp.getDouble(6);
          double costo=resp.getDouble(8);
          txt_precio.setText(String.valueOf(normal));
          txt_costo.setText(String.valueOf(costo));
          txt_nuevo.setText(String.valueOf(normal));
    
    } catch (SQLException ex) 
        {
        ex.printStackTrace();
        }
        
    }
  
    public void llenoVentanaCrea(String codigo){
          txt_precio.setText(ventcreaprod.txt_precio.getText());
          txt_costo.setText(ventcreaprod.txt_costo.getText());
          txt_nuevo.setText(ventcreaprod.txt_precio.getText());
    }
    
    
    
    
    public void operacion1(){
       //Valor precio Normal = n
       double c=Double.parseDouble(txt_costo.getText()); 
       c=((c*100)/100)+c;
       txt_nuevo.setText(String.valueOf(c));
      //System.out.println("El calculo da:"+c);
    }
    public void operacion2(){
       //Valor precio Normal = n
       double c=Double.parseDouble(txt_costo.getText()); 
       c=((c*150)/100)+c;
       txt_nuevo.setText(String.valueOf(c));
      //System.out.println("El calculo da:"+c);
    }
    public void operacion3(){
       //Valor precio Normal = n
       double c=Double.parseDouble(txt_costo.getText()); 
       c=((c*200)/100)+c;
       txt_nuevo.setText(String.valueOf(c));
      //System.out.println("El calculo da:"+c);
    }
    
    
    public void calcularPrecio(){
    int opcion=cb_opciones.getSelectedIndex();
    try{
     if(opcion==1){
     operacion1();
     }
     if(opcion==2){
     operacion2();
     }
     if(opcion==3){
     operacion3();
     }
    }catch(Exception e){
    txt_nuevo.setText("0.00");
    }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bt_guardar;
    private javax.swing.JComboBox cb_opciones;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JLabel txt_codigo;
    private javax.swing.JTextField txt_costo;
    private javax.swing.JTextField txt_nuevo;
    private javax.swing.JTextField txt_precio;
    // End of variables declaration//GEN-END:variables
    
}
